package com.yhf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yhf.bean.Clazz;

import com.yhf.bean.Student;

import com.yhf.service.ClazzService;
import com.yhf.service.StudentService;
import com.yhf.utils.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/student")
public class StudentController {
    private Map<String,Object> result =new HashMap<>();
    @Autowired
    private StudentService studentService;
    @Autowired
    private ClazzService clazzService;
    @GetMapping("/goStudentListView")
    public String goStudentListView(Model  model) {
        List<Clazz> clazzList = clazzService.getClazzList(new Clazz());
        model.addAttribute(clazzList);
        return "student/studentList";
    }
    @PostMapping("/getStudentList")
    @ResponseBody
    public Map<String, Object> getStudentList(Integer page, Integer rows, String studentname,String clazzname) {

        Student student = new Student();
        student.setName(studentname);
        student.setClazz_name(clazzname);

        //设置每页记录数
        PageHelper.startPage(page, rows);


        //获取指定的和管理员或者是所有的管理员数据
        List<Student> studentList = studentService.getStudentList(student);

        PageInfo<Student> studentPageInfo = new PageInfo<>(studentList);
        //总页数

        long total = studentPageInfo.getTotal();
        result.put("total", total);
        result.put("rows", studentList);
        return result;

    }
    @PostMapping("/addStudent")
    @ResponseBody
    public Map<String, Object> addStudent(Student student) {
        Student user = studentService.findByName(student.getName());
        if (user == null) {
            //添加操作
            try {
                if (studentService.addStudent(student) > 0) {
                    result.put("success", true);
                } else {
                    result.put("success", false);
                    result.put("msg", "服务器发生异常,添加失败!");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            result.put("success", false);
            result.put("msg", "该用户名已存在,请修改后重试!");
        }
        return result;
    }
    @PostMapping("/editStudent")
    @ResponseBody
    public Map<String, Object> editStudent(Student student) {
        System.out.println("111");
        //需要排除用户名只修改账户名以外信息
        Student student1 = studentService.findByName(student.getName());
        System.out.println("222");
        if (student1 != null) {
            System.out.println("333");
            if (!(student.getId().equals(student1.getId()))) {
                System.out.println("444");
                result.put("success", false);
                result.put("msg", "该用户名已存在，请更改重试！");
                return result;
            }
        }
        System.out.println("555");
        //修改操作
        if (studentService.updateStudent(student) > 0) {
            System.out.println("666");
            result.put("success", true);
        } else {
            System.out.println("777");
            result.put("success", false);
            result.put("msg", "服务器异常，修改失败！");
        }
        return result;

    }
    @PostMapping("/deleteStudent")
    @ResponseBody
    public Map<String, Object> deleteStudent(@RequestParam(value = "ids[]", required = true) Integer[] ids) {
        if (studentService.deleteStudent(ids) > 0) {
            result.put("success", true);
        } else {
            result.put("success", false);
            result.put("msg", "服务器异常，删除失败！");
        }
        return result;
    }
    @PostMapping("/uploadPhoto")
    @ResponseBody
    public Map<String,Object> uploadPhoto(MultipartFile photo, HttpServletRequest request){
        final  String realPath = request.getServletContext().getRealPath("/uplod/admin_portrait/");
        final String portraitPath = request.getServletContext().getContextPath() + "/uplod/admin_portrait/";
        return UploadFile.getUploadResult(photo,realPath,portraitPath);
    }

}
